import Vue from 'vue'
import Router from 'vue-router'
import constantRouterMap from './env'
import store from '../store'
import _ from 'lodash'

Vue.use(Router)

let thisRouter = new Router({
  mode: 'hash',
  routes: constantRouterMap
})

// 前置守卫
thisRouter.beforeEach((to, from, next) => {
  let activeMenu = to.meta.activeMenu
  if (!activeMenu) {
    console.error('此路由没有acriveMenu')
  } else {
    store.dispatch('setActiveMenu', activeMenu)
  }
  if (!to.meta.required) {
    next()
    return
  }
  if (process.env.NODE_ENV !== 'production') {
    next()
    return
  }
  let power = _.find(store.state.currentUser.permission, { 'name': String(to.name), 'type': '2' })
  if (power) {
    next()
  } else {
    Vue.prototype.$Message.info('无权限访问，请联系管理员调整权限')
    // next('/')
  }
})
// 路由跳转后scroll 位置
thisRouter.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
})

export default thisRouter
